
Partial Class admin_Budget
    Inherits System.Web.UI.Page

    Private value As String
    Private isError As Boolean

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        ClearStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.BudgetScrollingGridView.Sort("DisplayName", SortDirection.Ascending)
        End If

        Dim sDataKey(0) As String
        sDataKey(0) = "Id"
        Me.BudgetScrollingGridView.DataKeyNames = sDataKey

    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.BudgetScrollingGridView.DivClientId, CInt(Me.BudgetScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.BudgetScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(0)
        ScriptManager.RegisterClientScriptBlock(Me.BudgetScrollingGridView, Me.BudgetScrollingGridView.GetType(), _
        "scrollDivAsync", EntityController.JavaScriptScrollingGridStartup(Me.BudgetScrollingGridView), True)
    End Sub

    Protected Sub dvNew_ItemInserted(ByVal sender As Object, _
                                     ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) _
                                     Handles NewDetailsView.ItemInserted

        Me.BudgetObjectDataSource.Select()
        Me.BudgetScrollingGridView.DataBind()

    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles AddButton.Click

        Me.BudgetScrollingGridView.Visible = False
        Me.AddButton.Visible = False

        Me.NewObjectDataSource.Select()
        Me.NewDetailsView.DataBind()

        Me.NewDetailsView.Visible = True
        Me.SubmitButton.Visible = True
        Me.CancelButton.Visible = True

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles SubmitButton.Click

        Me.NewDetailsView.InsertItem(False)

        If Not isError Then

            If ErrorSummary.Visible = True Then
                ErrorSummary.Visible = False
            End If

            Me.NewDetailsView.Visible = False

            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False

            Me.BudgetScrollingGridView.Visible = True
            Me.AddButton.Visible = True

        End If

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles CancelButton.Click

        Me.NewDetailsView.Visible = False
        Me.SubmitButton.Visible = False
        Me.CancelButton.Visible = False

        Me.BudgetScrollingGridView.Visible = True
        Me.AddButton.Visible = True

    End Sub

    Protected Sub dvNew_ItemInserting(ByVal sender As Object, _
                                      ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
                                      Handles NewDetailsView.ItemInserting

        For i As Integer = 0 To e.Values.Count - 1

            If e.Values(i) Is Nothing Then
                value = Me.NewDetailsView.Rows(0).Cells(0).Text & " must be filled in before proceeding."
                Me.ErrorSummary.SetSingleError(value)
                e.Cancel = True
                Me.ErrorSummary.Visible = True
                isError = True
            ElseIf e.Values(i) IsNot Nothing Then
                e.Values(i) = Server.HtmlEncode(e.Values(i).ToString())
            End If

        Next

    End Sub

   


    Protected Sub BudgetScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles BudgetScrollingGridView.RowCancelingEdit

        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
        End If
        SetStatus("Update Cancelled.")
    End Sub

    Private Sub SetStatus(ByVal message As String)
        Me.BudgetScrollingGridView.StatusText = message
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub

    Protected Sub BudgetScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles BudgetScrollingGridView.RowDataBound
        If e.Row.DataItemIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(Me.BudgetScrollingGridView)
        End If

        AccessibilityController.SetGridViewEditToolTips(e.Row, Me.BudgetScrollingGridView)
        Dim txtDisplay As TextBox = CType(e.Row.FindControl("txtDisplay"), TextBox)
        If Not txtDisplay Is Nothing Then
            SetFocus(txtDisplay.ClientID)
        End If
        'Set whether number of lines is editable
        Dim numberOfLinesTextBox As TextBox = CType(e.Row.FindControl("NumberOfLinesTextBox"), TextBox)
        If Not numberOfLinesTextBox Is Nothing Then
            If numberOfLinesTextBox.Text = "0" Then
                numberOfLinesTextBox.Enabled = False
            Else
                numberOfLinesTextBox.Enabled = True
            End If
        End If
        'Set the values for the IsActive Checkbox or the image
        Dim category As Cdm.DataCore.LookupTables.LuAdminBudgetCategory = e.Row.DataItem
        Dim activeImage As Image = e.Row.FindControl("IsActiveImage")
        Dim aggregateImage As Image = e.Row.FindControl("UseAggregateImage")
        If Not activeImage Is Nothing Then
            If category.IsActive = True Then
                activeImage.ImageUrl = "~/images/greencheck.gif"
                activeImage.AlternateText = "true"
            Else
                activeImage.ImageUrl = "~/images/blankcheck.gif"
                activeImage.AlternateText = "false"
            End If
        Else
            Dim activeCheckbox As CheckBox = e.Row.FindControl("EditActiveCheckbox")
            If Not activeCheckbox Is Nothing Then
                activeCheckbox.Checked = category.IsActive
                activeCheckbox.InputAttributes.Add("title", "is category active?")
            End If
        End If

        If Not aggregateImage Is Nothing Then
            If category.UseAggregateExpenditures = True Then
                aggregateImage.ImageUrl = "~/images/greencheck.gif"
                aggregateImage.AlternateText = "true"
            Else
                aggregateImage.ImageUrl = "~/images/blankcheck.gif"
                aggregateImage.AlternateText = "false"
            End If
        Else
            Dim aggregateCheckbox As CheckBox = e.Row.FindControl("EditAggregateCheckbox")
            If Not aggregateCheckbox Is Nothing Then
                aggregateCheckbox.Checked = category.UseAggregateExpenditures
                aggregateCheckbox.InputAttributes.Add("title", "Check this to Enter all expenditure items for this category on one line.")
            End If
        End If

    End Sub

    Protected Sub BudgetScrollingGridView_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles BudgetScrollingGridView.RowDeleted
        SetStatus("Category Deleted")
    End Sub

    Protected Sub BudgetScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles BudgetScrollingGridView.RowUpdated
        SetStatus("Category Updated.")
    End Sub

    Protected Sub BudgetScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles BudgetScrollingGridView.RowUpdating
        If Not IsNumeric(e.NewValues.Item(1).ToString()) Then
            value = "Display Order value must be a valid number."
            Me.ErrorSummary.SetSingleError(value)
            e.Cancel = True
            Me.ErrorSummary.Visible = True
        ElseIf Not IsNumeric(e.NewValues.Item(2).ToString()) Then
            value = "# of lines must be a valid number."
            Me.ErrorSummary.SetSingleError(value)
            e.Cancel = True
            Me.ErrorSummary.Visible = True
        Else
            Dim newLines As Integer = Convert.ToInt32(e.NewValues.Item(2))
            Dim oldLines As Integer = Convert.ToInt32(e.OldValues.Item(2))
            If newLines < 0 Or (newLines = 0 And oldLines > 0) Then
                value = "# of lines must be greater than zero"
                Me.ErrorSummary.SetSingleError(value)
                e.Cancel = True
                Me.ErrorSummary.Visible = True
            Else
                If ErrorSummary.Visible = True Then
                    ErrorSummary.Visible = False
                End If
            End If
        End If
    End Sub
End Class
